{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Report counts of GO terms at various levels and depths\n",
    "\n",
    "Reports the number of GO terms at each level and depth.   \n",
    "\n",
    "  * **Level** refers to the length of the shortest path from the top.   \n",
    "  * **Depth** refers to the length of the longest path from the top.\n",
    "\n",
    "See the Gene Ontology Consorium's (GOC) advice regarding \n",
    "[levels and depths of a GO term](http://geneontology.org/faq/how-can-i-calculate-level-go-term)\n",
    "    \n",
    "## GO level and depth reporting\n",
    "\n",
    "GO terms reported can be all GO terms in an ontology.     \n",
    "Or subsets of GO terms can be reported.     \n",
    "GO subset examples include all GO terms annotated for a species or all GO terms in a study.\n",
    "\n",
    "Example report on full Ontology from Ontologies downloaded April 27, 2016.\n",
    "```\n",
    "Dep <-Depth Counts->  <-Level Counts->\n",
    "Lev   BP    MF    CC    BP    MF    CC\n",
    "--- ----  ----  ----  ----  ----  ----\n",
    "00     1     1     1     1     1     1\n",
    "01    24    19    24    24    19    24\n",
    "02   125   132   192   223   155   336\n",
    "03   950   494   501  1907   738  1143\n",
    "04  1952  1465   561  4506  1815  1294\n",
    "05  3376  3861   975  7002  4074   765\n",
    "06  4315  1788   724  7044  1914   274\n",
    "07  4646  1011   577  4948   906    60\n",
    "08  4150   577   215  2017   352     6\n",
    "09  3532   309   106   753   110     1\n",
    "10  2386   171    24   182    40     0\n",
    "11  1587   174     3    37    22     0\n",
    "12  1032    70     1     1     0     0\n",
    "13   418    53     0     0     0     0\n",
    "14   107    17     0     0     0     0\n",
    "15    33     4     0     0     0     0\n",
    "16    11     0     0     0     0     0\n",
    "```\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Download Ontologies, if necessary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  EXISTS: go-basic.obo\n"
     ]
    }
   ],
   "source": [
    "# Get http://geneontology.org/ontology/go-basic.obo\n",
    "from goatools.base import download_go_basic_obo\n",
    "obo_fname = download_go_basic_obo()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Download Associations, if necessary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  EXISTS: gene2go\n"
     ]
    }
   ],
   "source": [
    "# Get ftp://ftp.ncbi.nlm.nih.gov/gene/DATA/gene2go.gz\n",
    "from goatools.base import download_ncbi_associations\n",
    "gene2go = download_ncbi_associations()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Initialize GODag object"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "load obo file go-basic.obo\n",
      "46518"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "go-basic.obo: format-version(1.2) data-version(releases/2016-04-27)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " nodes imported\n"
     ]
    }
   ],
   "source": [
    "from goatools.obo_parser import GODag\n",
    "\n",
    "obodag = GODag(\"go-basic.obo\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Initialize Reporter class"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from goatools.rpt_lev_depth import RptLevDepth\n",
    "\n",
    "rptobj = RptLevDepth(obodag)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. Generate depth/level report for all GO terms"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dep <-Depth Counts->  <-Level Counts->\n",
      "Lev   BP    MF    CC    BP    MF    CC\n",
      "--- ----  ----  ----  ----  ----  ----\n",
      "00     1     1     1     1     1     1\n",
      "01    24    19    24    24    19    24\n",
      "02   125   132   192   223   155   336\n",
      "03   950   494   501  1907   738  1143\n",
      "04  1952  1465   561  4506  1815  1294\n",
      "05  3376  3861   975  7002  4074   765\n",
      "06  4315  1788   724  7044  1914   274\n",
      "07  4646  1011   577  4948   906    60\n",
      "08  4150   577   215  2017   352     6\n",
      "09  3532   309   106   753   110     1\n",
      "10  2386   171    24   182    40     0\n",
      "11  1587   174     3    37    22     0\n",
      "12  1032    70     1     1     0     0\n",
      "13   418    53     0     0     0     0\n",
      "14   107    17     0     0     0     0\n",
      "15    33     4     0     0     0     0\n",
      "16    11     0     0     0     0     0\n"
     ]
    }
   ],
   "source": [
    "rptobj.write_summary_cnts_all()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Copyright (C) 2016, DV Klopfenstein, H Tang. All rights reserved."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
